﻿using System;
using System.Data;
using System.Net;
using TSL.BLL;
using TSL.DAL;

namespace TSL.WcfResult
{
    public class User
    {
        public UserInfoResult Check(string username, string password)
        {
            string loginIp = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();

            TSL.Model.SYS_users userInfo = new TSL.BLL.SYS_users().GetModelByName(username);
            if (userInfo == null)
            {
                return new UserInfoResult(-5, "用户名不存在！"){LoginIp = loginIp};
            }

            if (TSL.Common.PassWord.deUserPW(password,-1) != userInfo.userpass)
            {
                var thisPwd = TSL.Common.PassWord.deUserPW(password);
                return new UserInfoResult(-6, "用户密码错误！") { LoginIp = loginIp };
            }

            if ((Convert.ToInt32(userInfo.state) == 100) && ((userInfo.userclass == 0xbb9) || (userInfo.userclass == 0xbba)))
            {
                userInfo.loginNum++;
                userInfo.LoginIP = loginIp;
                new TSL.BLL.SYS_users().UpdateLogin(userInfo);
                new TSL.BLL.publish().AddUserLoginLog(userInfo.username, userInfo.LoginIP, string.Empty);
                //DataTable table = new TSL.BLL.caipiao().GetHreadDateCache(userInfo.userclass);
                var r =  GetUserInfo(username);
                r.LoginIp = loginIp;
                r.Levels = userInfo.levels;
                return r;
            }
            else
            {
                return new UserInfoResult(-7, "该用户为非法用户！"){LoginIp = loginIp};
            }
        }
        public UserInfoResult GetUserInfo(string userName)
        {
            try
            {
                DataTable table = new TSL.BLL.GetUserInfo().GetListFromView("username='" + userName + "'").Tables[0];
                if (table.Rows.Count <= 0)
                {
                    return new UserInfoResult(-1, "未获取到用户数据");
                }

                UserInfoResult userinfor = new UserInfoResult();
                userinfor.Profit = table.Rows[0]["Profit"].ToString();
                userinfor.Usermoney = table.Rows[0]["Usermoney"].ToString();
                userinfor.Lockmoney = table.Rows[0]["Lockmoney"].ToString();
                return userinfor;
            }
            catch (Exception ex)
            {
                return new UserInfoResult(-1, "系统错误！");
            }
        }

        //public UserInfoResult ChangeUserfdNums(string fdNums)
        //{
        //    int IntfdNums = -1;
        //    try
        //    {
        //        IntfdNums = int.Parse(fdNums);
        //    }
        //    catch
        //    {
        //        return new UserInfoResult(-999, "非法入侵本次将会记录！");
        //    }


        //    try
        //    {
        //        SYS_users user = new SYS_users();
        //        user.ChangeFD(AsUser.username, IntfdNums);
        //        UserInfoResult userinfor = new UserInfoResult();
        //        return userinfor;
        //    }
        //    catch (Exception ex)
        //    {
        //        return new UserInfoResult(-1, "系统错误！");
        //    }
        //}

        /// <summary>
        /// 增加新用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userClass"></param>
        /// <param name="userRates"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        public UserInfoResult UserAddResult(string userName, string userClass, string userRates, string userPwd)
        {
            TSL.Model.SYS_users model = new TSL.BLL.SYS_users().GetModelByName(userName);
            double Rates = Convert.ToDouble(model.rate_dw);
            model.username = userName;
            model.userpass = TSL.Common.PassWord.deUserPW(userPwd);
            model.regfrom = "&" + userName + "&" + model.regfrom;
            model.rate_bdw = Convert.ToDouble(model.rate_dw.ToString()) - Convert.ToDouble(userRates);
            model.rate_dw = Convert.ToDouble(userRates);
            model.userclass = Convert.ToInt32(userClass);

            bool flag = new TSL.BLL.SYS_users().Exists(model.username);

            if (string.IsNullOrEmpty(model.username))
            {
                return new UserInfoResult(-1, "用户名不能为空！");
            }

            if (string.IsNullOrEmpty(model.userpass))
            {
                return new UserInfoResult(-1, "用户密码不能为空！");
            }

            if (model.rate_dw > Rates)
            {
                return new UserInfoResult(-1, "返点不能超过自己！");
            }

            if (flag)
            {
                return new UserInfoResult(-1, "用户名已经存在！");
            }

            new TSL.BLL.SYS_users().QTAdd(model);

            return new UserInfoResult();
        }

    }
}
